package au.com.bus4u.controller;

import au.com.bus4u.service.UserService;
import au.com.bus4u.utils.PageData;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@Log
public class EmailController extends BaseController  {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserService userService;

    /**
     * 接收用户注册效验邮件
     * http://localhost:8080/validationUserEmail/user_id
     * @param user_id
     * @return
     * @throws Exception
     */
    @RequestMapping("/validationUserEmail/{user_id}")
    public ModelAndView toLogin(@PathVariable String user_id)throws Exception{
        log.info("=========================================");
//        System.err.println("user_id = [" + user_id + "]");
        ModelAndView mv = new ModelAndView();

        int code = 0;
        String msg = "";
        String url = "";
        String url_msg = "";

        //通过user_id到数据库中查询是否有改用户，如果有则修改状态成0(0-有效 1-冻结 2-邮箱未激活)
        PageData pageData = new PageData();
        pageData.put("user_id",user_id);
        PageData byId = userService.findById(user_id);
//        System.out.println("byId = " + byId);
        if(byId!=null){
            int status = Integer.parseInt(byId.get("status").toString());
            if(status == 0) {
                //邮件已经被激活成功，返回提示信息请不要重复激活邮箱信息
                code = 1;
                msg = byId.get("user_email")+",已经被激活成功，请不要重复激活!";
                url = "/login.html";
                url_msg = "点击跳转登陆页面";

            }else if(status == 1){
                //提示该账号已被冻结
                code = 1;
                msg = "非法操作:账号:"+byId.get("user_email")+",已被冻结!!!";
                url = "/login.html";
                url_msg = "点击跳转登陆页面";

            }else if(status == 2){
                code = 0;
                msg = byId.get("user_email")+",您已完成注册邮件的验证!!!";
                url = "/login.html";
                url_msg = "点击跳转登陆页面";

//                pageData.put("status","0");
//                userService.edit(pageData);
                byId.put("status","0");
//                userService.edit(byId);
                userService.editStatusById(byId);
            }else{
                code = 1;
                msg = byId.get("user_email")+":状态代码异常!!!";
                url = "/login.html";
                url_msg = "点击跳转登陆页面";
            }

        }else{
            //如果没有查询到user_id的用户，则说明用户的邮箱不能接收邮件，返回注册失败信息
            code = 1;
            msg = "非法操作:注册邮件验证失败!!!";
            url = "/login.html";
            url_msg = "点击跳转登陆页面";

        }


        mv.addObject("code",code);
        mv.addObject("msg",msg);
        mv.addObject("url",url);
        mv.addObject("url_msg",url_msg);
        mv.setViewName("/pc/tips.html");
        return mv;
    }


}
